package com.ilink.groundservice.controller.AiX;/**
 * <h3>iLink-java-backend-zhangbo2</h3>
 *
 * @Description <p>微场景阶段与前端交互接口</p>
 * @Author ZhangBo
 * @Date 2025-01-16 10:19
 **/

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ilink.groundservice.entity.PO.AiX.AiXSceneAuthor;
import com.ilink.groundservice.entity.PO.AiX.AiXScenePhaseBase;
import com.ilink.groundservice.entity.VO.AiX.AiXPhaseResponse;
import com.ilink.groundservice.mapper.AiX.AiXSceneAuthorMapper;
import com.ilink.groundservice.service.AiX.AiXScenePhaseBaseService;
import com.ilink.ilinkapi.client.UserServiceClient;
import com.ilink.ilinkapi.dto.ground.AiX.AiXPhaseDTO;
import com.ilink.ilinkapi.dto.ground.AiX.AiXPhaseRequest;
import com.ilink.ilinkapi.dto.ground.AiX.AiXPhaseUpdateInformationRequest;
import com.ilink.ilinkapi.dto.ground.AiX.AiXPhaseUpdateWorkRequest;
import com.ilink.ilinkcommon.domain.R;
import com.ilink.ilinkcommon.domain.User;
import com.ilink.ilinkcommon.log.method.MethodLog;
import com.ilink.ilinkcommon.utils.BeanUtils;
import com.ilink.ilinkcommon.utils.UserContext;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName AiXPhaseController
 * @Description TODO
 * @Author ZhangBo
 * @Date 2025-01-16 10:19
 **/
@Slf4j
@RequestMapping("/ground/aix/phase")
@RestController
@RequiredArgsConstructor
public class AiXPhaseController {
    private final AiXScenePhaseBaseService aiXScenePhaseBaseService;
    private final UserServiceClient userServiceClient;

    /**
     * @Author ZhangBo
     * @Description 获取指定场景的所有阶段
     * @Date 10:55 2025/1/16
     * @Param [sceneId]
     * @return R
     **/
    @MethodLog("获取场景阶段信息")
    @GetMapping("/get_phase_list/{sceneId}")
    public R getPhaseList(@PathVariable Long sceneId) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, sceneId);
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("view")) {
                log.error("用户没有查看该场景的权限，userId: " + userId + ", sceneId: " + sceneId);
                return R.error("您没有查看该场景的权限");
            }
            // 调用服务层方法获取阶段信息
            List<AiXPhaseResponse> phaseList = aiXScenePhaseBaseService.getPhaseList(sceneId);
            if (phaseList.isEmpty()) {
                return R.error("未找到对应的阶段列表");
            }
            return R.ok("场景阶段列表获取成功", phaseList);
        } catch (Exception e) {
            log.error("获取场景阶段列表失败，sceneId: " + sceneId, e);
            return R.error("获取场景阶段列表失败");
        }
    }

    /**
     * @Author ZhangBo
     * @Description 更新阶段列表
     * @Date 11:10 2025/1/16
     * @Param [phaseRequestList]
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog("更新阶段列表")
    @PostMapping("/update_phase_list")
    public R updatePhaseList(@RequestBody AiXPhaseRequest phaseRequestList) {
        try {
            Long sceneId = phaseRequestList.getSceneId();
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, sceneId);
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("edit")) {
                log.error("用户没有编辑该场景的权限，userId: " + userId + ", sceneId: " + sceneId);
                return R.error("您没有编辑该场景的权限");
            }
            List<AiXPhaseDTO> phaseDTO = phaseRequestList.getPhaseDTOList();
            // 调用服务层方法更新阶段列表
            return aiXScenePhaseBaseService.updatePhaseList(sceneId, phaseDTO);
        } catch (Exception e) {
            log.error("更新阶段列表失败: " + e.getMessage(), e);
            return R.error("更新阶段列表失败");
        }
    }

    /**
     * @Author ZhangBo
     * @Description 根据阶段ID获取阶段信息
     * @Date 16:03 2025/1/16
     * @Param [phaseId]
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog("获取阶段信息")
    @GetMapping("/get_phase")
    public R getPhase(@RequestParam("sceneId") Long sceneId, @RequestParam("phaseId") Long phaseId) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, sceneId);
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("view")) {
                log.error("用户没有查看该阶段的权限，userId: " + userId + ", phaseId: " + phaseId);
                return R.error("您没有查看该阶段的权限");
            }
            AiXScenePhaseBase phase = aiXScenePhaseBaseService.getPhaseById(phaseId);
            if (phase == null) {
                return R.error("未找到该阶段信息");
            }
            return R.ok("获取阶段信息成功", phase);
        } catch (Exception e) {
            log.error("获取阶段信息失败，phaseId: " + phaseId, e);
            return R.error("获取阶段信息失败");
        }
    }

    @MethodLog("更新阶段介绍")
    @PostMapping("/update_phase_instruction")
    public R updatePhaseInstruction(@RequestBody AiXPhaseUpdateInformationRequest request) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, request.getSceneId());
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("edit")) {
                log.error("用户没有编辑该阶段的权限，userId: " + userId + ", phaseId: " + request.getPhaseId());
                return R.error("您没有编辑该阶段的权限");
            }
            return aiXScenePhaseBaseService.updatePhaseInstructionById(request);
        } catch (Exception e) {
            log.error("更新阶段介绍失败，id: " + request.getPhaseId(), e);
            return R.error("更新阶段介绍失败");
        }
    }

    @MethodLog("更新阶段目标")
    @PostMapping("/update_phase_target")
    public R updatePhaseTarget(@RequestBody AiXPhaseUpdateInformationRequest request) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, request.getSceneId());
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("edit")) {
                log.error("用户没有编辑该阶段的权限，userId: " + userId + ", phaseId: " + request.getPhaseId());
                return R.error("您没有编辑该阶段的权限");
            }
            return aiXScenePhaseBaseService.updatePhaseTargetById(request);
        } catch (Exception e) {
            log.error("更新阶段目标失败，id: " + request.getPhaseId(), e);
            return R.error("更新阶段目标失败");
        }
    }

    @MethodLog("更新作业信息")
    @PostMapping("/update_work")
    public R updateWork(@RequestBody AiXPhaseUpdateWorkRequest request) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, request.getSceneId());
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("edit")) {
                log.error("用户没有编辑该阶段的权限，userId: " + userId + ", phaseId: " + request.getPhaseId());
                return R.error("您没有编辑该阶段的权限");
            }
            return aiXScenePhaseBaseService.updateWorkById(request);
        } catch (Exception e) {
            log.error("更新作业信息失败，phaseId: " + request.getPhaseId(), e);
            return R.error("更新作业信息失败");
        }
    }

    @MethodLog("删除作业")
    @PostMapping("/delete_work")
    public R deleteWork(@RequestParam("sceneId") Long sceneId, @RequestParam("phaseId") Long phaseId) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, sceneId);
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("edit")) {
                log.error("用户没有编辑该阶段的权限，userId: " + userId + ", phaseId: " + phaseId);
                return R.error("您没有编辑该阶段的权限");
            }
            return aiXScenePhaseBaseService.deleteWorkByPhaseId(phaseId);
        } catch (Exception e) {
            log.error("删除作业失败，phaseId: " + phaseId, e);
            return R.error("删除作业失败");
        }
    }

    @MethodLog("新增作业")
    @PostMapping("/insert_work")
    public R insertWork(@RequestParam("sceneId") Long sceneId, @RequestParam("phaseId") Long phaseId) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, sceneId);
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("edit")) {
                log.error("用户没有编辑该阶段的权限，userId: " + userId + ", phaseId: " + phaseId);
                return R.error("您没有编辑该阶段的权限");
            }
            return aiXScenePhaseBaseService.insertWorkByPhaseId(phaseId);
        } catch (Exception e) {
            log.error("新增作业失败，phaseId: " + phaseId, e);
            return R.error("新增作业失败");
        }
    }

}
